# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>

_realname=oslo-concurrency
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
provides=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
replaces=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=4.4.0
pkgrel=1
pkgdesc="OpenStack library for all concurrency-related code (mingw-w64)"
arch=('any')
url='https://pypi.python.org/pypi/oslo.concurrency'
license=('Apache')
depends=("${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-python-six"
         "${MINGW_PACKAGE_PREFIX}-python-pbr"
         "${MINGW_PACKAGE_PREFIX}-python-oslo-config"
         "${MINGW_PACKAGE_PREFIX}-python-oslo-i18n"
         "${MINGW_PACKAGE_PREFIX}-python-oslo-utils"
         "${MINGW_PACKAGE_PREFIX}-python-fasteners")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-setuptools")
checkdepends=("${MINGW_PACKAGE_PREFIX}-python-mock"
              "${MINGW_PACKAGE_PREFIX}-python-oslotest"
              "${MINGW_PACKAGE_PREFIX}-python-oslo-config"
              "${MINGW_PACKAGE_PREFIX}-python-eventlet"
              "${MINGW_PACKAGE_PREFIX}-python-testrepository")
options=('staticlibs' 'strip' '!debug')
install=${_realname}3-${CARCH}.install
source=("${_realname}-${pkgver}.tar.gz::https://github.com/openstack/oslo.concurrency/archive/${pkgver}.tar.gz")
sha512sums=('85e1f3c999c2d5455fc05d2ae98f45086dcc055157897356abd2afe631ed78bd609e1e2336e26891f24a1a245ae9da08fbf6ffdb102e1508ff64063d0b425c62')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ]; then
      rm -rf $_fname
    fi
  done
}
# =========================================== #

prepare() {
  cd "${srcdir}/oslo.concurrency-${pkgver}"

  cd "${srcdir}"
  rm -rf python-build-${CARCH} | true
  cp -r "oslo.concurrency-${pkgver}" "python-build-${CARCH}"

  # Set version for setuptools_scm
  export SETUPTOOLS_SCM_PRETEND_VERSION=${pkgver}
  export PBR_VERSION=${pkgver}
}

build() {
  cd "${srcdir}/python-build-${CARCH}"
  ${MINGW_PREFIX}/bin/python setup.py build
}

check() {
  cd "${srcdir}/python-build-${CARCH}"
  ${MINGW_PREFIX}/bin/python setup.py test || warning "Tests failed"
}

package() {
  cd "${srcdir}/python-build-${CARCH}"

  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE"

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  # fix python command in files
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*.py; do
    sed -e "s|/usr/bin/env |${MINGW_PREFIX}|g" \
        -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i ${_f}
  done
}
